package com.myComsole.deepseek;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @author: hfxiang
 * @Description: 专注获取某次对话用户方的发言（必须是每次都深度思考） ds一个对话总字数限制为8万4左右，ds一般是用户发言的3倍这样，不考虑一次会话大小限制的话最极限可以发言到8万字，想办法精简，或者分段让ds总结压缩多余的信息
 * @date: 2025/16/18 16:45
 */
public class dealDsResult {
    public static void main(String[] arg) {
        String sourcePath = "D:\\deepseek对话\\conversations.json";
        String resultPath = "D:\\deepseek对话\\result.txt";
        if (FileUtil.exist(resultPath)) {
            FileUtil.del(resultPath);
        }
        File file = new File(sourcePath);
        String text = FileUtil.readString(file, Charset.forName("utf-8"));
        JSONArray jsonArray = JSONUtil.parseArray(text);
        JSONObject lastSession = selectByTitle(jsonArray,"红衣感情事件分析与自我");
        List<String> myContents = selectMyWordList(lastSession);
        String resultText = dealMyContents(myContents);
        FileUtil.writeString(resultText, resultPath, Charset.forName("utf-8"));
    }
    private static String dealMyContents(List<String> myContents){
        StringBuilder content = new StringBuilder();
        for (String myContent : myContents) {
            if (myContent.contains("这是我最近和deepseek的一次会话")){
                continue;
            }
            myContent = myContent.trim().replace("\r\n","。");
            myContent = myContent.trim().replace("\n","。");
            myContent = myContent.trim().replace("\t","。");
            content.append(myContent).append("。");
        }
        return content.append(".").toString();
    }

    private static JSONObject selectByTitle(JSONArray jsonArray,String title){
        for (Object o : jsonArray) {
            JSONObject jsonObject = (JSONObject) o;
            if (jsonObject.getStr("title").contains(title)){
                return jsonObject;
            }
        }
        return null;
    }
    private static JSONObject selectLast(JSONArray jsonArray){
        JSONObject lastObject =(JSONObject) jsonArray.get(jsonArray.size() - 1);
        return lastObject;
    }
    private static List<String> selectMyWordList(JSONObject sessionObject){
        JSONObject mapping = sessionObject.getJSONObject("mapping");
        List<String> myContents = new ArrayList<>();
        Collection<Object> objects = mapping.values();
        for (Object object : objects) {
            JSONObject jsonObject = (JSONObject) object;
            if (jsonObject.get("message")==null) {
                continue;
            }
            JSONObject message = jsonObject.getJSONObject("message");
            //判断有没有思考过程(仅思考模式才能这样)
            if (message.get("reasoning_content")!=null) {
                continue;
            }
            //获取对话内容
            String myContent = message.getStr("content");
            if (message.get("files")!=null&& !message.getJSONArray("files").isEmpty()) {
                myContent = message.getJSONArray("files").getJSONObject(0).getStr("content");
            }
            myContents.add(myContent);
        }
        return myContents;
    }

}
